package com.demomaster.quickcompat.widget.popup;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.util.DisplayUtil;
import com.demomaster.quickcompat.widget.base.QuickGravity;

import java.io.Serializable;
import java.lang.ref.WeakReference;

public class QuickPopupWindow extends PopupWindow {
    Context context;
    private Direction direction;

    public Context getContext() {
        return context;
    }

    public QuickPopupWindow(Context context) {
        super(context);
    }

    public QuickPopupWindow(View contentView, int width, int height) {
        super(contentView, width, height);
    }

    public QuickPopupWindow(Context context, Builder builder) {
        this.context = context;
        direction = builder.direction;
        if (builder.contentView != null) {
            setContentView(builder.contentView);
        } else if (builder.layoutResID != Resources.ID_NULL) {
            View view = LayoutInflater.from(this.context).inflate(builder.layoutResID, null, false);
            setContentView(view);
        }
        setWidth(builder.width);
        setHeight(builder.height);
        setOutsideTouchable(builder.outsideTouchable);
    }

    public enum Direction {
        top, left, right, bottom
    }

    public static class Builder<T extends Builder> implements Serializable {
        public int animationStyleID = -1;
        public WeakReference<Context> contextWeakReference;
        public View contentView;
        public int layoutResID = R.layout.quick_layout_popup_tip;
        public int backgroundColor = Color.BLACK;
        public float[] backgroundRadius = new float[8];
        private Direction direction;
        private int width = ViewGroup.LayoutParams.WRAP_CONTENT;
        private int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        private int popupWidth = -1;
        private boolean withArrow = true;
        private int arrowWidth;
        private int arrowHeight;
        public boolean mTouchable = true;
        public boolean outsideTouchable;
        public QuickGravity quickGravity = QuickGravity.TOP;

        public Builder(Context context) {
            this.contextWeakReference = new WeakReference<>(context);
            arrowWidth = DisplayUtil.dip2px(context, 8);
            arrowHeight = DisplayUtil.dip2px(context, 8);
            setBackgroundRadius(DisplayUtil.dip2px(context, 5));
        }

        public T setBackgroundColor(int backgroundColor) {
            this.backgroundColor = backgroundColor;
            return (T) this;
        }

        public T setTouchable(boolean mTouchable) {
            this.mTouchable = mTouchable;
            return (T) this;
        }

        public T setBackgroundRadius(float backgroundRadiu) {
            if (backgroundRadius == null) {
                return (T) this;
            }
            for (int i = 0; i < backgroundRadius.length; i++) {
                this.backgroundRadius[i] = backgroundRadiu;
            }
            return (T) this;
        }

        public T setBackgroundRadius(float[] backgroundRadius) {
            this.backgroundRadius = backgroundRadius;
            return (T) this;
        }

        public T setWidth(int width) {
            this.width = width;
            return (T) this;
        }

        public T setHeight(int height) {
            this.height = height;
            return (T) this;
        }

        public T setArrowWidth(int arrowWidth) {
            this.arrowWidth = arrowWidth;
            return (T) this;
        }

        public T setArrowHeight(int arrowHeight) {
            this.arrowHeight = arrowHeight;
            return (T) this;
        }

        public T setWithArrow(boolean withArrow) {
            this.withArrow = withArrow;
            return (T) this;
        }

        public T setAnimationStyleID(int animationStyleID) {
            this.animationStyleID = animationStyleID;
            return (T) this;
        }

        public T setOutsideTouchable(boolean outsideTouchable) {
            this.outsideTouchable = outsideTouchable;
            return (T) this;
        }

        public T setGravity(QuickGravity quickGravity) {
            this.quickGravity = quickGravity;
            return (T) this;
        }

        public T setDirection(QDTipPopupWindow.Direction direction) {
            this.direction = direction;
            return (T) this;
        }

        public T setPopupWidth(int popupWidth) {
            this.popupWidth = popupWidth;
            return (T) this;
        }

        public T setContentView(View contentView) {
            this.contentView = contentView;
            return (T) this;
        }

        public T setContentView(int layoutResID) {
            this.layoutResID = layoutResID;
            return (T) this;
        }

        public QuickPopupWindow create() {
            return new QuickPopupWindow(contextWeakReference.get(), this);
        }
    }
}
